package com.starblink.login.changeboundemail.inputemail.ui

import com.alibaba.android.arouter.facade.annotation.Route
import com.starblink.android.basic.base.activity.BaseTVMActivity
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.navigationTo
import com.starblink.android.basic.user.login.LoginManager
import com.starblink.basic.route.RoutePage
import com.starblink.basic.style.title.OnTitleBarListener
import com.starblink.basic.style.title.TitleBar
import com.starblink.login.databinding.ActivityChangeBoundEmailInputEmailBinding

/**
 * 修改绑定邮箱-输入邮箱
 */
@Route(path = RoutePage.Login.CHANGE_BOUND_EMAIL_INPUT_EMAIL)
class ChangeBoundEmailInputEmailActivity :
    BaseTVMActivity<ActivityChangeBoundEmailInputEmailBinding, ChangeBoundEmailInputEmailVM>() {

    override fun initViewBinding(): ActivityChangeBoundEmailInputEmailBinding =
        ActivityChangeBoundEmailInputEmailBinding.inflate(layoutInflater)

    override fun initView() {
        super.initView()
        viewBinding.titleBar.setOnTitleBarListener(object : OnTitleBarListener {
            override fun onLeftClick(titleBar: TitleBar?) {
                finish()
            }
        })

        viewBinding.etEmail.setOnEditInputListener {
            viewBinding.tvErrorHint.visibleOrGone(false)
            viewBinding.btnContinue.isEnabled = it.isNotEmpty()
        }

        viewBinding.btnContinue.isEnabled = viewBinding.etEmail.text.isNotEmpty()
        viewBinding.btnContinue.click {
            val res = LoginManager.validateEmail(viewBinding.etEmail.text)
            if (res == null) {
                // 输入邮箱OK，接口验证
                viewModel.checkEmail(viewBinding.etEmail.text)
            } else {
                viewBinding.tvErrorHint.visibleOrGone(true)
                viewBinding.tvErrorHint.text = res
            }
        }
    }

    override fun initObservable() {
        super.initObservable()
        viewModel.checkResultLiveData.observe(this) {
            if (it == true) {
                navigationTo(RoutePage.Login.CHANGE_BOUND_EMAIL_VERIFY_EMAIL){
                    withString(LoginManager.KEY_EMAIL, viewBinding.etEmail.text)
                }
                return@observe
            }
        }
    }
}